package utils

import (
	"ksirGo/global"
	"math/rand"
	"strconv"
	"time"
)

func generateCode(length int) string {
	rand.Seed(time.Now().UnixNano())
	var code string
	for i := 0; i < length; i++ {
		code += strconv.Itoa(rand.Intn(10))
	}
	return code
}

func CreateMessageCode(key string) string {
	var code = generateCode(6)
	global.REDIS.Set(ctx, key, code, 60*time.Second)
	return code
}

func VerifyMessageCode(key string, value string) bool {
	if target, err := global.REDIS.Get(ctx, key).Result(); err != nil {
		return false
	} else {
		return target == value
	}
}
